package csc.fresher.view;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.InterestRate;
import csc.fresher.entity.PeriodType;
import csc.fresher.service.CurrencyService;
import csc.fresher.service.InterestRateService;
import csc.fresher.service.PeriodTypeService;

@Component
@Scope("request")
public class SavingInterestRateManagedBean {

	@Autowired
	InterestRateService interestRateService;

	@Autowired
	CurrencyService currencyService;

	@Autowired
	PeriodTypeService periodTypeService;

	private List<InterestRate> usdRates;

	private List<InterestRate> vndRates;

	private List<PeriodType> periodTypes;

	public List<InterestRate> getUsdRates() {
		return usdRates;
	}

	public void setUsdRates(List<InterestRate> usdRates) {
		this.usdRates = usdRates;
	}

	public List<InterestRate> getVndRates() {
		return vndRates;
	}

	public void setVndRates(List<InterestRate> vndRates) {
		this.vndRates = vndRates;
	}

	@PostConstruct
	public void init() {
		usdRates = new ArrayList<>();
		vndRates = new ArrayList<>();
		if (periodTypeService != null && interestRateService != null) {
			periodTypes = periodTypeService.getAllTypes();
			if (periodTypes != null) {
				for (PeriodType p : periodTypes) {
					if (p != null) {
						usdRates.add(interestRateService
								.getNewestInterestRateByCurrencyPeriod(1,
										p.getPeriodTypeID()));
						vndRates.add(interestRateService
								.getNewestInterestRateByCurrencyPeriod(2,
										p.getPeriodTypeID()));
					}
				}
			}

			if (interestRateService != null) {
				if (usdRates != null) {
					for (InterestRate i : usdRates) {
						if (currencyService != null) {
							i.setCurrency(currencyService.getCurrencyByID(i
									.getCurrencyID()));
							System.out.println(i.getCurrencyID());
						} else {
							System.err.println("currency service is null");
						}
						if (periodTypeService != null) {
							i.setPeriodType(periodTypeService
									.getPeriodTypeByID(i.getPeriodTypeID()));
						} else {
							System.err.println("period service is null");
						}
					}
				}
				if (vndRates != null) {
					for (InterestRate i : vndRates) {
						if (currencyService != null) {
							i.setCurrency(currencyService.getCurrencyByID(i
									.getCurrencyID()));
							System.out.println(i.getCurrencyID());
						} else {
							System.err.println("currency service is null");
						}
						if (periodTypeService != null) {
							i.setPeriodType(periodTypeService
									.getPeriodTypeByID(i.getPeriodTypeID()));
						} else {
							System.err.println("period service is null");
						}
					}
				}
			} else {
				System.err.println("interest rate service is null");
			}
		} else {
			System.err.println("period Type Service is null");
		}

	}

}
